package com.a01_2021.thousandstimes.general.service.impl;

import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.exception.CustomExceptionType;
import com.a01_2021.thousandstimes.general.entity.AbilityModel;
import com.a01_2021.thousandstimes.general.mapper.AbilityModelMapper;
import com.a01_2021.thousandstimes.general.service.ExperienceService;
import com.a01_2021.thousandstimes.general.service.IAbilityModelService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author E1xP
 * @since 2021-03-31
 */
@Service
public class AbilityModelServiceImpl extends ServiceImpl<AbilityModelMapper, AbilityModel> implements IAbilityModelService {

    @Resource
    AbilityModelMapper abilityModelMapper;

    @Resource
    ExperienceService experienceService;

    /**
     * 获取全部能力模型
     *
     * @return List 能力模型实体类
     */
    @Override
    public List<AbilityModel> getAllAbilityModel() {
        QueryWrapper<AbilityModel> queryWrapper = new QueryWrapper<>();
        return abilityModelMapper.selectList(queryWrapper);
    }

    /**
     * 通过职位标签id获取能力模型
     *
     * @param postId 职位分类id
     * @return 能力模型实体类
     * @throws CustomException 当能力模型不存在时抛出该异常
     */
    @Override
    public AbilityModel getFromPostId(long postId) throws CustomException {
        QueryWrapper<AbilityModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("post_id", postId);
        AbilityModel abilityModel = abilityModelMapper.selectOne(queryWrapper);
        if (abilityModel == null) {
            throw new CustomException(CustomExceptionType.ABILITY_MODEL_NOT_FOUND, "找不到该能力模型");
        }
        return abilityModel;
    }

    /**
     * 通过职位id获取能力模型
     *
     * @param positionId 职位id
     * @return 能力模型实体类
     * @throws CustomException 当能力模型不存在时抛出该异常
     */
    @Override
    public AbilityModel getFromPositionId(long positionId) throws CustomException {
        QueryWrapper<AbilityModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("position_id", positionId);
        AbilityModel abilityModel = abilityModelMapper.selectOne(queryWrapper);
        if (abilityModel == null) {
            throw new CustomException(CustomExceptionType.ABILITY_MODEL_NOT_FOUND, "找不到该能力模型");
        }
        return abilityModel;
    }

    /**
     * 更新能力模型
     *
     * @param abilityModel 更新的能力模型
     * @return 能力模型
     * @throws CustomException 当传入的能力模型不存在时抛出该异常
     */
    @Override
    public AbilityModel updateAbilityModel(AbilityModel abilityModel) throws CustomException {
        AbilityModel dataBaseModel = abilityModelMapper.selectById(abilityModel.getId());
        if (dataBaseModel == null) {
            throw new CustomException(CustomExceptionType.ABILITY_MODEL_NOT_FOUND, "找不到该能力模型");
        }
        abilityModelMapper.updateById(abilityModel);
        return abilityModel;
    }

    /**
     * 获取用户当前的对应模型
     *
     * @param userId 用户id
     * @return 能力模型
     */
    public AbilityModel getByUserId(long userId) throws CustomException {
        String currentTitle = experienceService.getCurrentTitle(userId);
        QueryWrapper<AbilityModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("positon_id", experienceService.getPositionId(userId));
        return abilityModelMapper.selectOne(queryWrapper);
    }

    /**
     * 从主数据库获取能力模型
     *
     * @param postId 职位标签id
     * @return AbilityModel
     */
    @DS("master")
    @Override
    public AbilityModel getFromMaster(Integer postId) {
        return abilityModelMapper.selectById(postId);
    }

    /**
     * 将能力模型插入数据库
     *
     * @param abilityModel 能力模型
     */
    @Override
    public void insert(AbilityModel abilityModel) {
        abilityModelMapper.insert(abilityModel);
    }
}
